package webhandler;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import database.*;
import REUtils.*;
import globalpara.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CollectData {

	public static byte GetURLTable(String stockno,String TableType,String Quarter){
				
    	    String Strstart= null;
        	String Strlast = null;

    	    String StrURL= new String("null");  
    	    Strref StrURLref = new Strref();
	    	
	    	if(TableType=="isq"){       //損益表（季表）
	    		StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	     	    showMsg.DEBUG("GetBKURLStr(): "+StrURL); 
	    		 if(StrURL != null){
	    	       Strstart = "營業收入淨額";
	    	       Strlast  = "當季特別股息負債";
                   ParsingSQData(StrURL,stockno,TableType,Strstart,Strlast);
	    		 }else{return 0;}
	    	}else if(TableType=="cq"){  //現金流量表 (季表)
	    		StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
	    	       Strstart = "稅後淨利";
	    	       Strlast  = "本期支付所得稅";  
                   ParsingSQData(StrURL,stockno,TableType,Strstart,Strlast);
	    		 }else{return 0;}
	    	}else if(TableType=="bsq"){ //資產負債表（季表）
	    		StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
			      Strstart = "現金及約當現金";
			      Strlast  = "負債及股東權益總額";			      
                  ParsingSQData(StrURL,stockno,TableType,Strstart,Strlast);
	    	    }else{return 0;}  
	    	}else if(TableType=="cap"){ //股本形成
	    		StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
		           ParsingCAP(StrURL,stockno); 
	    		 }else{return 0;}
		      
		    }else if(TableType=="inc"){ //月營收明細
		    	StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
			      ParsingInc(StrURL, stockno);
		        }else{return 0;}
			}else if(TableType=="is"){ //損益表（年表） 
			  //StrURL = StrURL + StrIS ;
 
			}else if(TableType=="bs"){ //資產負債表（年表） 
			 // StrURL = StrURL + StrBS ;
 
			}else if(TableType=="fr"){ //財務比率（年表）
			  //StrURL = StrURL + StrFR ;
 
			}else if(TableType=="ra"){ //財務比率（季表）
				StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
			      ParsingQRate(StrURL, stockno);
				}else{return 0;}
 
			}else if(TableType=="ho"){ //董監事經理人及大股東持股明細
				StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
			      ParsingHOLD(StrURL, stockno, TableType,Quarter);
			    }
			}else if(TableType=="ba"){ //基本資料
				StrURL = GetBKURLStr(StrURLref, stockno, TableType); 
	    		 if(StrURL != null){
			      ParsingBasic(StrURL, stockno, TableType,Quarter);
                }else{return 0;}
             }
    				     
		   	showMsg.INFO("stockno,TableType,Quarter= "+ stockno+","+TableType+","+Quarter);
		   	showMsg.INFO(TableType+" "+"StrURL "+": "+ StrURL);
		   	return 1 ;
		  			    
	}//end GetURLTable
	
  	
    public static String GetBKURLStr(Strref RefURL,String stockno,String TableType){
    	
    	String RetURL= null;
	    String StrURL="http://dj.mybank.com.tw/z/zc/";
       
		String StrASPend = ".asp.htm";
		String Strdjthm = ".djhtm";
		
		String Str1CFSQ = "zc3/zc3_";       //現金流量表 (季表)
		String Str1ISQT = "zcq/zcq_";       //損益表（季表）
		String Str1BSQ  = "zcp/zcpa/zcpa_"; //資產負債表（季表）
		String Str1CAP  = "zcb/zcb_";       //股本形成
		String Str1INC  = "zch/zch_";       //月營收明細
		String Str1IS   = "zcq/zcqa_";      //損益表（年表） 
		String Str1BS   = "zcp/zcpb/zcpb_"; //資產負債表（年表） 
		String Str1FR   = "zcr/zcra/zcra_"; //財務比率（年表） 
		String Str1RATE  = "zcr/zcr_";      //財務比率（季表）
        String Str1HOLD  = "zck/zck_";	    //董監事經理人及大股東持股明細
        String Str1BASIC = "zca/zca_";      //基本資料
		
		String StrCAP   = Str1CAP+stockno+StrASPend;
    	String StrISQ   = Str1ISQT+stockno+StrASPend;
    	String StrBSQ   = Str1BSQ+stockno+StrASPend;
    	String StrCFSQ  = Str1CFSQ+stockno+Strdjthm;
    	String StrINC   = Str1INC+stockno+Strdjthm;
    	String StrIS    = Str1IS+stockno+StrASPend;
    	String StrBS    = Str1BS+stockno+StrASPend;	    	
      	String StrFR    = Str1FR+stockno+StrASPend;	
    	String StrRATE  = Str1RATE+stockno+Strdjthm;
    	String StrHOLD  = Str1HOLD+stockno+Strdjthm;
    	String StrBASIC = Str1BASIC+stockno+StrASPend;	
    	
    	    if(TableType=="isq"){       //損益表（季表）
    		    RetURL   = StrURL + StrISQ ;

	    	}else if(TableType=="cq"){  //現金流量表 (季表)
	    		RetURL = StrURL + StrCFSQ ;
	    		
	    	}else if(TableType=="bsq"){ //資產負債表（季表）
	    		RetURL = StrURL + StrBSQ ;
	    		
	    	}else if(TableType=="cap"){ //股本形成
	    		RetURL = StrURL + StrCAP ;
 
		    }else if(TableType=="inc"){ //月營收明細
		    	RetURL = StrURL + StrINC ;
		
			}else if(TableType=="is"){ //損益表（年表） 
				RetURL = StrURL + StrIS ;

			}else if(TableType=="bs"){ //資產負債表（年表） 
				RetURL = StrURL + StrBS ;

			}else if(TableType=="fr"){ //財務比率（年表）
				RetURL = StrURL + StrFR ;

			}else if(TableType=="ra"){ //財務比率（季表）
				RetURL = StrURL + StrRATE ;

			}else if(TableType=="ho"){ //董監事經理人及大股東持股明細
				RetURL = StrURL + StrHOLD ;

			}else if(TableType=="ba"){ //基本資料
				RetURL = StrURL + StrBASIC ;
           }else{
        	    RetURL = "GetSrcURL(): TableType is error !";
        	    return RetURL ;
           }
    	   
    	   RefURL.str = RetURL ;
    	   //showMsg.DEBUG("GetBKURLStr(): "+RefURL.str); 

    	   return RetURL ;
    }
	
	
 //2010/09/26
 //ppkliu	
 //To Get the Calendar from URL,if DataStore is not new Quarter or Month information
 //	
    public static byte GetMonths(String INCURL,List<String> ListMon,String StockNo){
    	//http://dj.mybank.com.tw/z/zc/zch/zch_2705.djhtm
    	  
    	 int firsthit = 0;
 		String line=null;
 		String lineTmp=null;
 		int k1=0,k2=0;
 		int lineno=0;			
 		int i=0 ;
 		int MaxQnum = 3 ;               //To get Max items number
        String Keyitem = "達成率" ;		//from hit line to get 3 months data

    	
  		  try { 
			  URL TableForm = new URL(INCURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	    	  
	    	  while((line=reader.readLine())!=null &&(i<MaxQnum) ){     		  
	    		  
	    		  if(line.indexOf(Keyitem)>0 && (firsthit == 0)){ //To get the keyitem line No.
	    			  firsthit =1;
	    		  }

		    	    if(i<MaxQnum && (firsthit == 1)){	    
				         showMsg.DEBUG("GetMonths: show line "+lineno+" "+i+" "+line);	
					     
				         i++;
			    			  String patternStr = ">[-+]?[0-9]*\\.?[0-9]*[Q]?</";	//only for Quarter items
						      Pattern pattern = Pattern.compile(patternStr);
	  
						    	  Matcher matcher = pattern.matcher(line);
							      boolean matchFound = matcher.find();
							      
							      if(matchFound){

							    	  k1=matcher.start();
							    	  k2=matcher.end();
							    	  lineTmp = line.substring(k1+1,k2-2);
							    	  ListMon.add(lineTmp);				    	  
							    	  line = line.substring(k2,line.length());					    	 
							      }//if		    			  	    		
			    	    }//end if MaxQnum
				     lineno++; //next line
	    		  }	//end of while
		     		    		  			
	    	 
		      reader.close();  //close URL
		      
	   	  	  //end of try
		      } catch(ArrayIndexOutOfBoundsException e){
		    	  showMsg.ERR("GetMonths(): Array error");
		    	  return 1 ;
		      } catch (MalformedURLException e) {
		    	  showMsg.ERR("GetMonths():URL = "+INCURL);
		    	  showMsg.ERR("GetMonths(): You Must enter correct URL");
		    	  return 2 ;
		      } catch (IOException e) {
		    	  showMsg.ERR("GetMonths: IO Error:"+e.getMessage());
	              e.printStackTrace();	
		    	  return 3 ;
		      }finally {	  
			  	  i=0;  	

		    	  //Last8Q = ListQitem ;
		    	  //showMsg.prnList("GetCalendar()",Last8Q);
			  	showMsg.DEBUG("GetMonths: show line  finish");	
			  }//finally 	
    	
    	
    	return 0 ;
      }
    
//2010/09/03
//ppkliu	
//To Get the Calendar from URL,if DataStore is not new Quarter or Month information
//	
    public static byte GetCalendar(String StrURL,List<String> ListQitem){
	  
        int firsthit = 0;
		String line=null;
		String lineTmp=null;
		int k1=0,k2=0;
		int lineno=0;			
		int i=0 ;
		int MaxQnum = 9 ;
        String Keyitem = "期別" ;		
		//List<String> Last8Q = new ArrayList<String>();
		
 	    //showMsg.DEBUG("GetCalendar(): "+StrURL); 
		
		  try { 
			  URL TableForm = new URL(StrURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	    	  
	    	  while((line=reader.readLine())!=null &&(i<MaxQnum) ){     		  
	    		  
	    		  if(line.indexOf(Keyitem)>0 && (firsthit == 0)){ //the first Quarter items
	    			  firsthit =1;
	    		  }

		    	    if(i<MaxQnum && (firsthit == 1)){	    
				         //showMsg.INFO("GetCalendar: show line "+lineno+" "+i+" "+line);	
					     i++;
			    			  String patternStr = ">[-+]?[0-9]*\\.?[0-9]*[Q]?</";	//only for Quarter items
						      Pattern pattern = Pattern.compile(patternStr);
	  
						    	  Matcher matcher = pattern.matcher(line);
							      boolean matchFound = matcher.find();
							      
							      if(matchFound){

							    	  k1=matcher.start();
							    	  k2=matcher.end();
							    	  lineTmp = line.substring(k1+1,k2-2);
							    	  ListQitem.add(lineTmp);				    	  
							    	  line = line.substring(k2,line.length());					    	 
							      }//if		    			  	    		
			    	    }//end if MaxQnum
				     lineno++; //next line
	    		  }	//end of while
		     		    		  			
	    	 
		      reader.close();  //close URL
		      
	   	  	  //end of try
		      } catch(ArrayIndexOutOfBoundsException e){
		    	  showMsg.ERR("GetCalendar(): Array error");
		    	  return 1 ;
		      } catch (MalformedURLException e) {
		    	  showMsg.ERR("GetCalendar():URL = "+StrURL);
		    	  showMsg.ERR("GetCalendar(): You Must enter correct URL");
		    	  return 2 ;
		      } catch (IOException e) {
		    	  showMsg.ERR("GetCalendar: IO Error:"+e.getMessage());
	              e.printStackTrace();	
		    	  return 3 ;
		      }finally {	  
			  	  i=0;  	

		    	  //Last8Q = ListQitem ;
		    	  //showMsg.prnList("GetCalendar()",Last8Q);
			  }//finally
	  
	  
	  return 0 ;
  }	//end of GetCalendar()
	
  public static void ParsingHOLD(String StrURL,String stockno,String TableType,String sThisMonth
    ){
	  
   int  hitline =0;
   int  startline = 0;
   int  FlagStop = 0 ;
   String line=null;
   String lineTmp=null;
   String item=null ;
   String Stopitem = null;
   int k1=0,k2=0;
   int lineno=0;			

   //Store to Database
   String sKey="0" ;
   List<String> ListHOLD = new ArrayList<String>();

  try { 
	  URL TableForm = new URL(StrURL); // Read from big5 page
	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	  //i=0;
	  item = "持股比例";
	  Stopitem = "來自年報的";
	  
	  while((line=reader.readLine())!=null ){ 
		   		 		  
		  if(line.indexOf(Stopitem)>0){
			  hitline = 0 ;
			  FlagStop = 1;
			  //System.out.println("stop "+line);
		  }else if(lineno == startline &&(startline != 0) && (FlagStop != 1) ){  //Start fetch data
			  hitline =1 ;
		  }else if(line.indexOf(item)>0){ //the first line
			  startline = lineno+3;			 
		  }
		  
		  if((lineno < startline + 6) && hitline == 1 ){
		   k1 = line.lastIndexOf("\">");
		   k2 = line.lastIndexOf("</");
		   lineTmp = line.substring(k1+2,k2);
		   ListHOLD.add(lineTmp);
		  }
	  
		  if(lineno == startline + 6 && (hitline == 1) && (startline !=0)){ //Restart fetch data
			  hitline = 0;
			  startline += 8; 
		  }
	  						  
		  lineno++;
		  
      }    	    	  	    	  	    	  
      reader.close();
      		      
	  	  //end of try
      } catch (MalformedURLException e) {
    	  System.out.println("ParsingHOLD: You Must enter correct URL");
      } catch (IOException e) {
	      System.out.println("ParsingHOLD: IO Error:"+e.getMessage());	
          e.printStackTrace();	
      }finally {
          //After Get URL data
    	  showMsg.INFO("ParsingHOLD("+stockno+","+TableType+") finally");			  
	  
		  if(!ListHOLD.isEmpty()){//store total BASIC information
			  sKey = TableType+stockno+"."+sThisMonth ;  			          			   
			  StoreDB(sKey,ListHOLD);  //Store to Database
			  
		      showMsg.INFO("ParsingHOLD: sKey:"+sKey);
		      
		    }
	  	   }	  

}//ParsingHOLD

	
	public static void ParsingSQData(String StrURL,String stockno,String TableType,
			                          String Strstart,String Strlast){

		String line=null;			 
		int k1=0,k2=0;
		int lineno=0;			
		int i=0, j=0,r=0;
		int seasonline=0;
		int startline=0;
		int lastline=0;
		
		//Store to Database
		String sKey="0" ;
		List<String> ListSQ = new ArrayList<String>();
		List<String> ListRet = new ArrayList<String>();
		List<String> ListStore = new ArrayList<String>();
		
	    try {  	  
			
	    	//Get the Report URL
	    	  URL TableForm = new URL(StrURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));

	    	  
	    	  //if(reader != null){
		      while((line=reader.readLine())!=null ){ 

		    	  if(line.indexOf("期別") >0){
		    		  seasonline = lineno;
		    	  }
		    	  if(line.indexOf(Strstart) >0){
		    		  startline = lineno ;
		    	  }
		    	  if(line.indexOf(Strlast) >0){
		    		  lastline = lineno ;
		    	  }
		    	  		    	 		    	  
		    	  //Parsing last 8 Quarter items
		          if(lineno > (seasonline) &&(seasonline !=0) && (lineno < (seasonline+9)) ){  
		        	  k1 = line.indexOf(">");
			       	  k2 = line.lastIndexOf("<");
			       	  ////////
			       	  //String stemp = line.substring(k1+1,k2);
			       	  //showMsg.DEBUG(TableType+" getSQ:"+i+" "+stemp);
			       	  
			       	  
			       	  ListSQ.add(i,line.substring(k1+1,k2));
			       	  i++;       	  
		          }
	
		          if(!((lastline !=0)&&(startline > lastline)) ){
		          if( lastline ==0 || ((lineno > lastline) )   ){  
		            if(lineno > startline && (startline !=0) ){		        	  
		        	  k1 = line.indexOf(">");
			       	  k2 = line.lastIndexOf("</t");
			          line = line.substring(k1+1,k2);
			          //showMsg.DEBUG("L "+line);
			          
			          k1 = line.indexOf(">");
			          k2 = line.length();
			          
		          
			          if(k1 != -1){
			        	  line=line.substring(k1+1,k2);
			          }
			       	  //String stemp = line.substring(k1+1,k2);
			       	  //showMsg.DEBUG("getSQ:"+i+" "+stemp);
			          //showMsg.DEBUG(i+" "+"k1:"+k1+" "+k2);
			          
			       	  ListSQ.add(i,line);			       	  
		        	  i++;  
		        	  j++;
		        	  if(j==8){j=0; startline += 11; }
		            }
		           }
		          }
		    	  lineno++;
		       }   
		      reader.close();
	   	  	  //end of try
//	          } catch (reader == null){
		      } catch (MalformedURLException e) {
		    	  showMsg.ERR("ParsingSQData():You Must enter correct URL");
		    	  showMsg.ERR("ParsingSQData():"+StrURL);
		      } catch (IOException e) {
		    	  showMsg.ERR("ParsingSQData(): IO Error:"+e.getMessage());	
	              e.printStackTrace();	
		      }finally {
	              //After Get URL data
			  	  showMsg.INFO("ParsingSQData("+stockno+","+TableType+") finally");
			  	  
			  	  showMsg.INFO(TableType+" "+"ParsingSQData():"+StrURL);

			  if(ListSQ.isEmpty()){showMsg.WARN("ListSQ.isEmpty() !!");}
                  else{
			  	  //re-sort the data
			  	   for(j=0;j<8;j++){
			  	    for(i=j;i<ListSQ.size();i=i+8){
			  		  ListRet.add(ListSQ.get(i));
			  	    }
			  	   }
		   	 
			  	  //Store to database
			  	 if(ListRet.isEmpty()){showMsg.ERR("ListRet.isEmpty() !!");}
			  	   
				     r = ListRet.size()/8 ;
				     for(j=0;j<ListRet.size();j=j+r){
					  sKey = TableType+stockno+"."+ListRet.get(j) ; 
    			      ListStore=ListRet.subList(j, j+r);//removeAll();
	
				   if(ListStore.isEmpty()){showMsg.ERR("sq"+j+": "+sKey+" is empty");}//ListSQ.isEmpty()	
				       
				      /*k1=0;
					  for(String sValue1 : ListStore) {
					    showMsg.DEBUG(TableType+" "+k1+": "+sValue1);
				    	  k1++;
				      }*/
					  
				       StoreDB(sKey,ListStore);  //Store to Database            
				    }
                  }			  			  	  
		    }	
	}//end of ParsingURLData

	public static byte ParsingBasic(String StrURL,String stockno,String TableType,String Quarter){
		
        int  hitline =0;
        int  startline = 0;
		String line=null;
		String lineTmp=null;
		String item=null ;
		int k1=0,k2=0;
		int lineno=0;			
		int i=0, j=0;

		
		//Store to Database
		String sKey="0" ;
		List<String> ListBA = new ArrayList<String>();

		
		  try { 
			  URL TableForm = new URL(StrURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	    	  TextUtils.initBAitems(); //initialize BASIC items
	    	  i=0;
	    	  
	    	  showMsg.DEBUG("ParsingBasic():Entry");
	    	  
	    	  while((line=reader.readLine())!=null ){ 
	    		    if(i<57){
	    		       item = TextUtils.getBAitems(i);
	    		    }
	    		    

	    		    
		    		if(line.indexOf(item)>0){
		    			startline = lineno;
                        hitline = lineno + 1 ;
		    			if(i==0 ){//print BASIC Date
			    		   k1 = line.indexOf(":");
						   k2 = line.indexOf("&nbsp");
						   lineTmp = line.substring(k1+1,k2);						      
						   ListBA.add(lineTmp);
						   i++ ;
						   startline = 0;
			    		 }else if(i==15){
			    		   k1 = line.lastIndexOf("\">");
						   k2 = line.lastIndexOf("</");
						   lineTmp = line.substring(k1+2,k2);
						   ListBA.add(lineTmp);
						   //showMsg.DEBUG("ParsingBasic():"+k1+"a,"+k2+": "+lineTmp);
						   startline = 0;	
						   i++;
                         }else if(i==53 || i ==54 || i==55 || i==56){
                           j=7;	 //start get 8 rowdata
                         }; //special
			    		  
		    		  }else if((lineno == hitline) &&(startline != 0) &&(i!=0)){

				       k1 = line.indexOf("\">");
			       	   k2 = line.lastIndexOf("</");
			       	   lineTmp = line.substring(k1+2,k2);
			       	  			       	  
			       	   k1 = lineTmp.indexOf("\">");
			       	   k2 = lineTmp.lastIndexOf("</");

			       	    if(k1>0 && (k2>0)){
			       	     lineTmp = lineTmp.substring(k1+2,k2);
			       	    }		       	  		       	  
			       	   ListBA.add(lineTmp);
		    		   //showMsg.DEBUG("ParsingBasic(): item i= "+i+" "+item );
			       	   
                       if(j!=0){  //Run state
                    	 hitline++;
                    	 j--;
                       }else{
    			        i++ ;
                       }
				   }
	    		  		    		  							  
				  lineno++;
				  
		      }    	    	  	    	  	    	  
		      reader.close();
		      		      
	   	  	  //end of try
		      } catch (MalformedURLException e) {
		    	  showMsg.ERR("ParsingBasic(): You Must enter correct URL");
		    	  return 0;
		      } catch (IOException e) {
		    	  showMsg.ERR("ParsingBasic(): IO Error:"+e.getMessage());	
	              e.printStackTrace();
	              return 0;
		      }finally {
	              //After Get URL data
	    	  
		    	  showMsg.WARN("ParsingBasic("+stockno+","+TableType+") finally");			  
			  	   if(!ListBA.isEmpty()){//store total BASIC information
				    	  showMsg.DEBUG("ParsingBasic() LisBA size:"+ListBA.size());
				    	  
				    	  /*j=0;
				    	  for(String stemp:ListBA){
				    		  showMsg.DEBUG("ParsingBasic() LisBA :"+j+" "+stemp);
				    		  j++;
				    	  }*/
				    	 
							  sKey = TableType+stockno+"."+Quarter ;  			          			   
							  StoreDB(sKey,ListBA);  //Store to Database						      
				      
					      return 1;
					    }
				 
			  	   }	  
		   
		      return 1;
	}//ParsingBasic
	
	
	public static byte StoreDB(String sKey,List<String> ListBA){
	   int i =0;
	   int j =0;
	   String sTemp = sKey ;
	   int MaxListSize = Gpara.MaxDBListSize;
	   int ListBAsize = ListBA.size();
	   int iStartidx = 0;
	   int iEndidx = 0 ;

	   //showMsg.DEBUG("StoreDB(sKey):ListBA.size()= "+sKey+" "+ListBAsize);
	   
		 if(ListBA.size() > MaxListSize){ //divide the ListBA
    		  List<String> ListStore = new ArrayList<String>();
    		  j = (ListBAsize/MaxListSize )+1;
    		 
    		 for(iStartidx=0;iStartidx<ListBAsize;iStartidx=iStartidx+MaxListSize){
    		  	 
    			 
    		  if((iStartidx+MaxListSize)>ListBAsize){  //Last List item

    			 iEndidx = ListBAsize ;
    		  }else{
    			 
    			 iEndidx = (iStartidx+MaxListSize);
    		  }
    			  
    		  ListStore = ListBA.subList(iStartidx, iEndidx);
    		  if(i != 0){
    		    sKey = sTemp+i ;
    		  }
   		       //showMsg.DEBUG("StoreDB():sKey= "+sKey);
   		       //showMsg.DEBUG("StoreDB():ListStore.size()= "+ListStore.size());
    		   //showMsg.DEBUG("StoreDB():iStartidx, iEndidx= "+iStartidx+" ,"+iEndidx);

    		  /* if(ListBA.size() == 35 ){
    			   showMsg.DEBUG("show ListStore");
    			   j=0;
    			   for(String ss:ListStore){
    				   showMsg.DEBUG(j+" ListStore "+ ss);
    				   j++; 
    			   }
    			   j=0;
    			   for(String ss:ListBA){
    				   showMsg.DEBUG(j+" ListBA "+ ss);
    				   j++;
    			   }   			   
    		   }*/
    		   
    		   BigTableUtils.addRowTo(sKey,ListStore);  //Store to Database	
    		   i++ ;
    		 } 
    		 
		 }else{			          			   
		      BigTableUtils.addRowTo(sKey,ListBA);  //Store to Database						      
    	  }
		 return 1;
	}
	
	
	public static void ParsingInc(String StrURL,String stockno){

	        int hitline =0;
			String line=null;
			String lineTmp=null;
			int k1=0,k2=0;
			int lineno=0;			
			int i=0, j=0;
			
			//Store to Database
			String sKey="0" ;
			List<String> ListINC = new ArrayList<String>();
			List<String> ListStore = new ArrayList<String>();
			
			  try { 
				  URL TableForm = new URL(StrURL); // Read from big5 page
		    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
		    	  
		    	  while((line=reader.readLine())!=null ){ 
		    	
		    		  if(line.indexOf("達成率") >0){
		    			  hitline = lineno +2;
			    	  }
		    		  
		    		  if(line.indexOf("/table>")>0 &&(hitline !=0)){	hitline =0;
		    		  }

		    		  		    		  
		    		  if(hitline != 0 &&(lineno == hitline))
		    		  {		    					    			

		    			while(line.length()>5){
		    			 k1 = line.indexOf("\">");
						 k2 = line.indexOf("</td>");
						 lineTmp = line.substring(k1+2,k2);	
						 ListINC.add(lineTmp);						 
						 line = line.substring(k2+5,line.length());
							
		    			}
		    			    
		    			hitline++;

		    		  }	  
		    		  		    		     		    		  							  
					  lineno++;					  
			      }    
				  			  
			      reader.close();  //close URL
			      
		   	  	  //end of try
			      } catch(ArrayIndexOutOfBoundsException e){
			    	  showMsg.ERR("ParsingInc: Array error");
			      } catch (MalformedURLException e) {
			    	  showMsg.ERR("ParsingInc: You Must enter correct URL");
			      } catch (IOException e) {
			    	  showMsg.ERR("ParsingInc: IO Error:"+e.getMessage());	
		              e.printStackTrace();	
			      }finally {
			    	  showMsg.INFO("ParsingInc("+stockno+") finally");			  
				  	
				  	   }	  
		    	  //年/月 	營業收入 	月增率 	去年同期 	年增率 	累計營收 	去年同期 	年增率 	達成率
			      //Regular Expression Pasing INC Report
			      String patternStr = "(?d)[0-9]/[0-9]";		      
			      Pattern pattern = Pattern.compile(patternStr);
			      i=0;
			      while(ListINC.size() > i) {
			    	  
			    	  Matcher matcher = pattern.matcher(ListINC.get(i));
				      boolean matchFound = matcher.find();
				      
				      if(matchFound){
				    	  sKey = "inc"+stockno+"."+ListINC.get(i);  //sKey Format

				    	  j=7;
				    	  i++;
				      }
				      if(j>0){
				    	  ListStore.add(ListINC.get(i));

				    	  j--;
				      }
				      if(ListStore.size()==7){

				    	  StoreDB(sKey,ListStore);  //Store to Database
				    	  ListStore.clear();
				      }
			    	  
			    	  i++;
			      
			        }
			  			      			  			      
	}//ParsingInc
	
	
	public static void ParsingCAP(String StrURL,String stockno){
        int hitline =0;
        int firsthit = 1;
		String line=null;
		String lineTmp=null;
		int k1=0,k2=0;
		int lineno=0;			
		int i=0;
		
		//Store to Database
		String sKey="0" ;
		List<String> ListCAP = new ArrayList<String>();
		List<String> ListStore = new ArrayList<String>();
		
		  try { 
			  URL TableForm = new URL(StrURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	    	  
	    	  while((line=reader.readLine())!=null ){ 
	    	
	    		  if(line.indexOf("公積及其他") >0 &&(firsthit == 1)){
	    			  hitline = lineno +3;
		    	  }
	    		  
	    		  if(line.indexOf("股本形成圖")>0 &&(hitline !=0)){
	    			  hitline =0;
	    		      firsthit = 0;
	    		  }
	    		  		    		  
	    		  if(hitline != 0 &&(lineno == hitline) )
	    		  {		    					    			
	    			  
	    			  String patternStr = ">[-+]?[0-9]*\\.?[0-9]*%?</";	//(>\\d+.\\d+<)(>\\d{1,4}<)	//">[-+]?[0-9]*\\.?[0-9]*%$</";
	    			  //(>\\d+\\.?\\d+<)|(>\\d+<)|(>\\d+<)(%<)
	    			  //[-+]?[0-9]*\\.?[0-9]* //float point
				      Pattern pattern = Pattern.compile(patternStr);

				      while(line.length()>4){ 	  
				    	  Matcher matcher = pattern.matcher(line);
					      boolean matchFound = matcher.find();
					      
					      if(matchFound){
					    	  k1=matcher.start();
					    	  k2=matcher.end();
					    	  lineTmp = line.substring(k1+1,k2-2);
					    	   ListCAP.add(lineTmp);
					    	  line = line.substring(k2,line.length());
					      }//if
	    			  }//while
	    			  	    			    		    
	    			hitline+=3;
	    		  }//if
				  lineno++;	
	    		  }	//while
	    		  		    		     		    		  							  			  			  
		      reader.close();  //close URL
		      
	   	  	  //end of try
		      } catch(ArrayIndexOutOfBoundsException e){
		    	  System.out.println("ParsingCAP: Array error");
		      } catch (MalformedURLException e) {
		    	  System.out.println("ParsingCAP: You Must enter correct URL");
		      } catch (IOException e) {
	    	      System.out.println("ParsingCAP: IO Error:"+e.getMessage());	
	              e.printStackTrace();	
		      }finally {
			  	  System.out.println("ParsingCAP("+stockno+") finally");			  
			  	  i=0;
				  if(!ListCAP.isEmpty()){//store total BASIC information
					  for(String sTMP1: ListCAP){ //show the List items

						  if(i%7 == 0 ){
							  sKey = "cap"+stockno+"."+ListCAP.get(i);  //sKey Format,year
						  }else{
							  ListStore.add(sTMP1);
						  }
						  
						  if(ListStore.size()==6){
							  StoreDB(sKey,ListStore);  //Store to Database
					    	  ListStore.clear();
					      }//for					  
                          i++;
				  	      
				  	      }//for
				    }		      
		        }
	}//ParsingCAP
	
	public static void ParsingQRate(String StrURL,String stockno){
        int hitline =0;
        int firsthit = 1;
		String line=null;
		String lineTmp=null;
		int k1=0,k2=0;
		int lineno=0;			
		int i=0;
		
		//Store to Database
		String sKey="0" ;
		List<String> ListQRate = new ArrayList<String>();
		List<String> ListStore = new ArrayList<String>();
		
		  try { 
			  URL TableForm = new URL(StrURL); // Read from big5 page
	    	  BufferedReader reader = new BufferedReader(new InputStreamReader(TableForm.openStream(),"Big5"));
	    	  
	    	  while((line=reader.readLine())!=null ){ 
	    	
	    		  if(line.indexOf("期別")>0 &&(firsthit ==1 )){ //the first Quarter items
	    			  hitline = lineno+1;
	    		      firsthit = 0;
	    		      i=0;
	    		  }
	    		  
	    		  if(line.indexOf("負債比率") >0 ){
	    			  hitline = lineno ;
	    			  i=0 ;
		    	  }
	    		  		    		  
	    		  if(hitline != 0 &&(lineno == hitline) )
	    		  {		    					    			    			  
	    			  String patternStr = ">[-+]?[0-9]*\\.?[0-9]*[Q]?</";	//only for Quarter finance Rate
				      Pattern pattern = Pattern.compile(patternStr);

				      while(line.length()>4){ 	  
				    	  Matcher matcher = pattern.matcher(line);
					      boolean matchFound = matcher.find();
					      
					      if(matchFound){

					    	  k1=matcher.start();
					    	  k2=matcher.end();
					    	  lineTmp = line.substring(k1+1,k2-2);
					    	  ListQRate.add(lineTmp);				    	  
					    	  line = line.substring(k2,line.length());					    	 
					      }//if
	    			  }//while
	    			  	    		
				      i++;
		    	    if(i<8){	    
	    			hitline++;
		    	    }else{hitline = 0;i=0;}
	    		  }//if
  				    lineno++;	
	    		  }	//while
	    		  		    		     		    		  							  			  			  
		      reader.close();  //close URL
		      
	   	  	  //end of try
		      } catch(ArrayIndexOutOfBoundsException e){
		    	  System.out.println("ParsingQRate: Array error");
		      } catch (MalformedURLException e) {
		    	  System.out.println("ParsingQRate: You Must enter correct URL");
		      } catch (IOException e) {
	    	      System.out.println("ParsingQRate: IO Error:"+e.getMessage());	
	              e.printStackTrace();	
		      }finally {
			  	  System.out.println("ParsingQRate("+stockno+") finally");			  
			  	  i=0;
			  	  
			  	if(!ListQRate.isEmpty()){//store total BASIC information
			  		 
					     ListStore=ListQRate.subList(8, 15);	  
			  		
			  		      sKey = "DebtRatio"+stockno+"."+ListQRate.get(0);
			  		      StoreDB(sKey,ListStore);  //Store to Database
				  					  
				  }//end of if
			  	  
			    }//finally
	}//ParsingQRate
	
}// end of class

/////////////////////////////////////////////////////////////////////////////

